using System.Collections.Generic;

/// <summary>
/// UI界面配置。
/// </summary>
public static partial class Config
{
    /// <summary>
    /// UI界面配置信息。
    /// </summary>
    public class UIFormInfo
    {
        /// <summary>
        /// 界面资源名称。
        /// </summary>
        public string AssetName { get; set; }

        /// <summary>
        /// 界面组名称。
        /// </summary>
        public string UIGroupName { get; set; }

        /// <summary>
        /// 是否暂停被覆盖的界面。
        /// </summary>
        public bool PauseCoveredUIForm { get; set; }

        /// <summary>
        /// 子界面ID列表（在打开主界面时自动加载）。
        /// </summary>
        public List<int> SubUIFormIds { get; set; }

        public UIFormInfo(string assetName, string uiGroupName, bool pauseCoveredUIForm = true)
        {
            AssetName = assetName;
            UIGroupName = uiGroupName;
            PauseCoveredUIForm = pauseCoveredUIForm;
            SubUIFormIds = new List<int>();
        }

        /// <summary>
        /// 添加子界面ID。
        /// </summary>
        /// <param name="subUIFormId">子界面ID。</param>
        public void AddSubUIFormId(int subUIFormId)
        {
            if (subUIFormId > 0 && !SubUIFormIds.Contains(subUIFormId))
            {
                SubUIFormIds.Add(subUIFormId);
            }
        }
    }

    /// <summary>
    /// UI界面配置。
    /// </summary>
    public static class UIForm
    {
        private static readonly Dictionary<int, UIFormInfo> s_UIFormConfigs = new Dictionary<int, UIFormInfo>();

        static UIForm()
        {
            InitializeConfigs();
        }

        /// <summary>
        /// 初始化UI界面配置。
        /// </summary>
        private static void InitializeConfigs()
        {
            // 提示类界面
            Register(UIFormId.LoadingTip, "UI/LoadingTip", UIGroup.Tip, true);
            Register(UIFormId.MessageTip, "UI/MessageTip", UIGroup.Tip, true);
            Register(UIFormId.CommonDialog, "UI/CommonDialog", UIGroup.Dialog, true);
            Register(UIFormId.TipDialog, "UI/TipDialog", UIGroup.Dialog, true);

            // 页面类界面
            Register(UIFormId.MainPage, "UI/MainPage", UIGroup.Page, true);
            Register(UIFormId.LoginPage, "UI/LoginPage", UIGroup.Page, true);
            Register(UIFormId.SettingPage, "UI/SettingPage", UIGroup.Page, true);
            Register(UIFormId.MainCityWindow, "Prefabs/UI/MainCityWindow/MainCityWindow", UIGroup.Form, true);

            // 对话框类界面
            Register(UIFormId.ConfirmDialog, "UI/ConfirmDialog", UIGroup.Dialog, true);
            Register(UIFormId.AlertDialog, "UI/AlertDialog", UIGroup.Dialog, true);
            Register(UIFormId.InputDialog, "UI/InputDialog", UIGroup.Dialog, true);
            Register(UIFormId.CardSelectDialog, "Prefabs/UI/CardSelectDialog/CardSelectDialog", UIGroup.Dialog, true);

            // 遮罩类界面
            Register(UIFormId.LoadingMask, "UI/LoadingMask", UIGroup.Mask, false);
            Register(UIFormId.BlockMask, "UI/BlockMask", UIGroup.Mask, false);
        }

        /// <summary>
        /// 注册UI界面配置。
        /// </summary>
        /// <param name="uiFormId">界面ID。</param>
        /// <param name="assetName">资源名称。</param>
        /// <param name="uiGroupName">界面组名称。</param>
        /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
        /// <param name="subUIFormIds">子界面ID列表（可选）。</param>
        /// <returns>UI界面配置信息。</returns>
        public static UIFormInfo Register(int uiFormId, string assetName, string uiGroupName, bool pauseCoveredUIForm = true, params int[] subUIFormIds)
        {
            if (s_UIFormConfigs.ContainsKey(uiFormId))
            {
                UnityEngine.Debug.LogWarningFormat("UIFormId {0} already registered, will override.", uiFormId);
            }

            UIFormInfo info = new UIFormInfo(assetName, uiGroupName, pauseCoveredUIForm);
            
            // 添加子界面ID
            if (subUIFormIds != null && subUIFormIds.Length > 0)
            {
                foreach (int subId in subUIFormIds)
                {
                    info.AddSubUIFormId(subId);
                }
            }

            s_UIFormConfigs[uiFormId] = info;
            return info;
        }

        /// <summary>
        /// 为已注册的界面添加子界面ID。
        /// </summary>
        /// <param name="uiFormId">界面ID。</param>
        /// <param name="subUIFormId">子界面ID。</param>
        public static void AddSubUIFormId(int uiFormId, int subUIFormId)
        {
            UIFormInfo info = GetUIFormInfo(uiFormId);
            if (info != null)
            {
                info.AddSubUIFormId(subUIFormId);
            }
            else
            {
                UnityEngine.Debug.LogWarningFormat("UIFormId {0} is not registered, cannot add sub UI form.", uiFormId);
            }
        }

        /// <summary>
        /// 获取UI界面配置。
        /// </summary>
        /// <param name="uiFormId">界面ID。</param>
        /// <returns>UI界面配置信息，如果不存在则返回null。</returns>
        public static UIFormInfo GetUIFormInfo(int uiFormId)
        {
            if (s_UIFormConfigs.TryGetValue(uiFormId, out UIFormInfo info))
            {
                return info;
            }

            return null;
        }

        /// <summary>
        /// 获取界面资源名称。
        /// </summary>
        /// <param name="uiFormId">界面ID。</param>
        /// <returns>界面资源名称，如果不存在则返回null。</returns>
        public static string GetAssetName(int uiFormId)
        {
            UIFormInfo info = GetUIFormInfo(uiFormId);
            return info?.AssetName;
        }

        /// <summary>
        /// 获取界面组名称。
        /// </summary>
        /// <param name="uiFormId">界面ID。</param>
        /// <returns>界面组名称，如果不存在则返回null。</returns>
        public static string GetUIGroupName(int uiFormId)
        {
            UIFormInfo info = GetUIFormInfo(uiFormId);
            return info?.UIGroupName;
        }

        /// <summary>
        /// 检查UI界面ID是否已注册。
        /// </summary>
        /// <param name="uiFormId">界面ID。</param>
        /// <returns>是否已注册。</returns>
        public static bool IsRegistered(int uiFormId)
        {
            return s_UIFormConfigs.ContainsKey(uiFormId);
        }
    }
}

